package com.guigu.erp.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.ManufactureApplyDto;
import com.guigu.erp.dto.Vo2;
import com.guigu.erp.pojo.*;
import com.guigu.erp.service.*;
import com.guigu.erp.utils.NumberUtils;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.ManufactureApplyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * ClassName:ManufactureApplyController
 * Package:com.guigu.erp.controller
 * Description:
 *
 * @Date:2024/3/21 20:38
 * @Author:chenchong
 */
@Controller
@RequestMapping("/ManufactureApply")
public class ManufactureApplyController {

    @Autowired
    private NumberUtils numberUtils;
    @Autowired
    private ManufactureApplyService manufactureApplyService;
    @Autowired
    private ManufactureDesignProcedureService manufactureDesignProcedureService;
    @Autowired
    private ManufactureDesignProcedureDetailsService manufactureDesignProcedureDetailsService;
    @Autowired
    private ManufactureDesignProcedureModuleService manufactureDesignProcedureModuleService;
    @Autowired
    private ManufactureManufactureService manufactureManufactureService;
    @Autowired
    private ManufactureProcedureService manufactureProcedureService;
    @Autowired
    private ManufactureProcedureModuleService manufactureProcedureModuleService;

    @RequestMapping("/add")
    @ResponseBody
    public String add(@RequestBody ManufactureApplyDto dto) {
        List<ManufactureApply> details = dto.getDetails();
        for (int i = 0; i < details.size(); i++) {
            ManufactureApply man = details.get(i);
            man.setId(null);
            String typeNumber = numberUtils.getTypeNumber("300");
            man.setApplyId(typeNumber);
            man.setType("Y001-1");
            man.setRemark(dto.getRemark());
            man.setRegister(dto.getRegister());
            man.setRegisterTime(dto.getRegisterTime());
            man.setCheckTag("S001-0");
            man.setManufactureTag("P001-0");
            manufactureApplyService.save(man);
        }
        return "ok";
    }

    @RequestMapping("/checkTagShow")
    @ResponseBody
    private List<ManufactureApply> checkTagShow() {
        QueryWrapper<ManufactureApply> qw = new QueryWrapper<>();
        qw.eq("CHECK_TAG", "S001-0");
        List<ManufactureApply> list = manufactureApplyService.list(qw);
        return list;
    }

    @RequestMapping("/checkTagShow1")
    @ResponseBody
    private List<ManufactureApply> checkTagShow1() {
        QueryWrapper<ManufactureApply> qw = new QueryWrapper<>();
        qw.eq("CHECK_TAG", "S001-1");
        qw.eq("MANUFACTURE_TAG", "P001-0");
        List<ManufactureApply> list = manufactureApplyService.list(qw);
        return list;
    }

    @RequestMapping("/checkTagShow2")
    @ResponseBody
    private List<ManufactureApply> checkTagShow2() {
        QueryWrapper<ManufactureApply> qw = new QueryWrapper<>();
        qw.eq("CHECK_TAG", "S001-1");
        qw.eq("MANUFACTURE_TAG", "P001-1");
        List<ManufactureApply> list = manufactureApplyService.list(qw);
        return list;
    }


    @RequestMapping("/getById")
    @ResponseBody
    private ManufactureApply getById(int ManufactureApplyId) {
        QueryWrapper<ManufactureApply> qw = new QueryWrapper<>();
        qw.eq("id", ManufactureApplyId);
        ManufactureApply one = manufactureApplyService.getOne(qw);
        return one;

    }

    @RequestMapping("/updateById")
    @ResponseBody
    private boolean updateById(ManufactureApply man) {
        boolean b = manufactureApplyService.updateById(man);
        return b;
    }

    @RequestMapping("/Show")
    @ResponseBody
    private PageUtils<ManufactureApply> Show(ManufactureApplyVo vo, int pageNo, int pageSize) {
        QueryWrapper<ManufactureApply> qw = new QueryWrapper<>();
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getApplyId())) {
                qw.eq("APPLY_ID", vo.getApplyId());
            }
            if (!StringUtils.isEmpty(vo.getCheckTag())) {
                qw.eq("CHECK_TAG", vo.getCheckTag());
            }

            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                qw.ge("REGISTER_TIME", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                qw.le("REGISTER_TIME", vo.getSearch_registerTime2());
            }
        }
        PageHelper.startPage(pageNo, pageSize);
        List<ManufactureApply> list = manufactureApplyService.list(qw);
        PageInfo<ManufactureApply> p = new PageInfo<>(list);
        PageUtils<ManufactureApply> utils = new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;

    }

    @RequestMapping("/arr")
    @ResponseBody
    private ManufactureApply arr(@RequestBody int[] val_text1) {
        BigDecimal b = new BigDecimal(0);
        ManufactureApply one = null;
        for (int i = 0; i < val_text1.length; i++) {
            QueryWrapper<ManufactureApply> qw = new QueryWrapper<>();
            qw.eq("id", val_text1[i]);
            one = manufactureApplyService.getOne(qw);
            BigDecimal amount = one.getAmount();
            b = b.add(amount);
        }
        ManufactureApply man = new ManufactureApply();
        man.setRegister(one.getRegister());
        man.setRegisterTime(one.getRegisterTime());
        man.setProductId(one.getProductId());
        man.setProductName(one.getProductName());
        man.setProductDescribe(one.getProductDescribe());
        man.setAmount(b);
        man.setRemark(one.getRemark());

        return man;
    }

    @RequestMapping("/arr2")
    @ResponseBody
    @Transactional
    public  String arr2(@RequestBody Vo2 vo2) {
   int[] val_text3=  vo2.getAra22();
        for (int i = 0; i < val_text3.length; i++) {
            ManufactureApply man = new ManufactureApply();
            man.setId(val_text3[i]);
            man.setManufactureTag("P001-1");
            manufactureApplyService.updateById(man);
        }
        BigDecimal b = new BigDecimal(0);
        BigDecimal b1 = new BigDecimal(0);
        ManufactureApply one = null;
        String buffer = "";
        for (int i = 0; i < val_text3.length; i++) {
            QueryWrapper<ManufactureApply> qw = new QueryWrapper<>();
            qw.eq("id", val_text3[i]);
            one = manufactureApplyService.getOne(qw);
            BigDecimal amount = one.getAmount();
            b = b.add(amount);
            buffer += one.getApplyId() + "/";
        }
        //查询工序信息
        QueryWrapper<ManufactureDesignProcedure> qw1 = new QueryWrapper<>();
        qw1.eq("PRODUCT_ID", one.getProductId());
        ManufactureDesignProcedure one1 = manufactureDesignProcedureService.getOne(qw1);
        ManufactureManufacture Manu = new ManufactureManufacture();
        String typeNumber = numberUtils.getTypeNumber("301");
        //生产总表
        Manu.setManufactureId(typeNumber);
        Manu.setProductId(one.getProductId());
        Manu.setProductName(one.getProductName());
        Manu.setAmount(b);
        Manu.setTestedAmount(b1);
        if (buffer.endsWith("/")) {
            buffer = buffer.substring(0, buffer.length() - 1);
        }
        Manu.setApplyIdGroup(buffer);
        Manu.setProductDescribe(one.getProductDescribe());
        BigDecimal mul = one1.getModuleCostPriceSum().multiply(b);
        Manu.setModuleCostPriceSum(mul);
        Manu.setRealModuleCostPriceSum(new BigDecimal(0));
        BigDecimal mul1 = one1.getCostPriceSum().multiply(b);
        Manu.setLabourCostPriceSum(mul1);
        Manu.setRealLabourCostPriceSum(new BigDecimal(0));
        Manu.setDesigner(vo2.getDesigner());
        Manu.setRegister(one1.getRegister());
        Manu.setRegisterTime(new Date());
        Manu.setCheckTime(new Date());
        Manu.setCheckTag("S001-0");
        Manu.setManufactureProcedureTag("S002-0");
        manufactureManufactureService.save(Manu);
        //生产工序表
        QueryWrapper<ManufactureDesignProcedureDetails> qw3 = new QueryWrapper<>();
        qw3.eq("PARENT_ID", one1.getId());
        List<ManufactureDesignProcedureDetails> list = manufactureDesignProcedureDetailsService.list(qw3);
        for (int i = 0; i < list.size(); i++) {
            ManufactureDesignProcedureDetails m = list.get(i);
            ManufactureProcedure man2 = new ManufactureProcedure();
            man2.setParentId(Manu.getId());
            man2.setDetailsNumber(m.getDetailsNumber());
            man2.setProcedureId(m.getProcedureId());
            man2.setProcedureName(m.getProcedureName());
            man2.setLabourHourAmount(m.getLabourHourAmount().multiply(b));
            man2.setRealLabourHourAmount(new BigDecimal(0));
            man2.setSubtotal(m.getSubtotal().multiply(b));
            man2.setRealSubtotal(new BigDecimal(0));
            man2.setRealAmount(new BigDecimal(0));
            man2.setDemandAmount(b);
            man2.setModuleSubtotal(m.getModuleSubtotal().multiply(b));
            man2.setRealModuleSubtotal(new BigDecimal(0));
            man2.setCostPrice(m.getCostPrice());
            man2.setProcedureFinishTag("G004-0");
            man2.setProcedureTransferTag("G005-0");
            manufactureProcedureService.save(man2);
            QueryWrapper<ManufactureDesignProcedureModule> qw4 = new QueryWrapper<>();
            qw4.eq("PARENT_ID", m.getId());
            List<ManufactureDesignProcedureModule> list1 = manufactureDesignProcedureModuleService.list(qw4);
            for (int j = 0; j < list1.size(); j++) {
                ManufactureDesignProcedureModule m2 = list1.get(j);
                ManufactureProcedureModule manuf = new ManufactureProcedureModule();
                manuf.setParentId(man2.getId());
                manuf.setDetailsNumber(m2.getDetailsNumber());
                manuf.setProductId(m2.getProductId());
                manuf.setProductName(m2.getProductName());
                manuf.setCostPrice(m2.getCostPrice());
                manuf.setAmount(m2.getAmount().multiply(b));
                manuf.setRealAmount(new BigDecimal(0));
//                manuf.setRenewAmount();
//                manuf.setRealAmount();
                manuf.setSubtotal(m2.getSubtotal().multiply(b));
                manuf.setRealSubtotal(new BigDecimal(0));
                manufactureProcedureModuleService.save(manuf);
            }
        }
        return "ok";
    }
}